#include<stdio.h>
#include<graphics.h>
#include<math.h>
#define PI 3.14159265358979323846


//画螺丝
//void drawScrew(float x, float y, float r)
//{
//	putimage()
//}

//画螺丝
void drawScrew(float x, float y, float r)
{
	float dx[5] = {10-r,r-10,10-r,r-10};
	float dy[5] = {10-r,r-10,r-10,10-r};
//	setfillcolor(EGERGB(170,170,170));
	for(int i=0;i<4;++i)
	{
		dx[i] += x;
		dy[i] += y;
		setcolor(WHITE);
		setlinestyle(SOLID_LINE,2);
		ege_ellipse(dx[i]-20,dy[i]-20,40.0,40.0);
		setcolor(BLACK);
		setlinestyle(SOLID_LINE,0,4);
		ege_line(dx[i]-10,dy[i],dx[i]+10,dy[i]);
		ege_line(dx[i],dy[i]-10,dx[i],dy[i]+10);		
	}
	
	setfillcolor(EGERGB(65,65,65));
	ege_fillellipse(x-150,y+90,14,14);
	setcolor(BLACK);
	setlinestyle(SOLID_LINE,0,2);
	ege_line(x-140,y+94,x-146,y+100);
	
	setfillcolor(EGERGB(65,65,65));
	ege_fillellipse(x+136,y+90,14,14);
	setcolor(BLACK);
	setlinestyle(SOLID_LINE,0,2);
	ege_line(x+140,y+100,x+146,y+94);
}

void drawBall(float x, float y, float r, float dx)
{
	setfillcolor(WHITE);
	ege_fillroundrect(x-130,y+70,260.0,50.0,10.0);
	setfillcolor(BLACK);
	ege_fillellipse(x+dx-15, y+72.5, 36.0, 44.0);
}

void writeWord(float x, float y, float r)
{
	setcolor(WHITE);
	LOGFONTW lf1;	// 定义 LOGFONTW 结构体变量
	ZeroMemory(&lf1, sizeof(LOGFONTW));	// 清空结构体	
	// 设置字体属性
	lf1.lfHeight = 25;           // 字体高度为 25 像素
	lf1.lfWeight = FW_BOLD;      // 字体粗细为粗体
	lf1.lfItalic = FALSE;        // 非斜体
	lf1.lfUnderline = FALSE;     // 无下划线
	lf1.lfStrikeOut = FALSE;     // 无删除线
	lstrcpyW(lf1.lfFaceName, L"黑体"); // 字体名称为黑体	
	setfont(&lf1);	// 设置字体
	ege_outtextxy((int)x-35,(int)y+120,"2  MIN");
	
	LOGFONTW lf2;	// 定义 LOGFONTW 结构体变量
	ZeroMemory(&lf2, sizeof(LOGFONTW));	// 清空结构体	
	// 设置字体属性
	lf2.lfHeight = 20;           // 字体高度为 20 像素
	lf2.lfWeight = FW_NORMAL;      // 字体粗细为正常
	lf2.lfItalic = FALSE;        // 非斜体
	lf2.lfUnderline = FALSE;     // 无下划线
	lf2.lfStrikeOut = FALSE;     // 无删除线
	lstrcpyW(lf2.lfFaceName, L"黑体"); // 字体名称为黑体	
	setfont(&lf2);	// 设置字体
	ege_outtextxy((int)x-43,(int)y-85,"D.OHANLEC");
	ege_outtextxy((int)x-85, (int)y-65,"TURN COORDINATOR");
	
	LOGFONTW lf3;	// 定义 LOGFONTW 结构体变量
	ZeroMemory(&lf3, sizeof(LOGFONTW));	// 清空结构体	
	// 设置字体属性
	lf3.lfHeight = 30;           // 字体高度为 30 像素
	lf3.lfWeight = FW_BOLD;      // 字体粗细为粗体
	lf3.lfItalic = FALSE;        // 非斜体
	lf3.lfUnderline = FALSE;     // 无下划线
	lf3.lfStrikeOut = FALSE;     // 无删除线
	lstrcpyW(lf3.lfFaceName, L"黑体"); // 字体名称为黑体	
	setfont(&lf3);	// 设置字体
	ege_outtextxy((int)x-80,(int)y-125,"L");
	ege_outtextxy((int)x+70,(int)y-125,"R");
	
	LOGFONTW lf4;	// 定义 LOGFONTW 结构体变量
	ZeroMemory(&lf4, sizeof(LOGFONTW));	// 清空结构体	
	// 设置字体属性
	lf4.lfHeight = 15;           // 字体高度为 10 像素
	lf4.lfWeight = FW_BOLD;      // 字体粗细为粗体
	lf4.lfItalic = FALSE;        // 非斜体
	lf4.lfUnderline = FALSE;     // 无下划线
	lf4.lfStrikeOut = FALSE;     // 无删除线
	lstrcpyW(lf4.lfFaceName, L"黑体"); // 字体名称为黑体	
	setfont(&lf4);	// 设置字体
	ege_outtextxy((int)x-30,(int)y+157,"MD  NTOR");
	ege_outtextxy((int)x-48,(int)y+170,"BEECHCRAFTBARON");
}

void drawBaseDashboard(float centerX, float centerY, float radius)
{
	setcolor(EGERGB(0, 0, 0));
	setfillcolor(EGERGB(0, 0, 0));  // 设置填充颜色
	ege_fillcircle(centerX, centerY, radius+25);  // 外圆
	
	setcolor(EGERGB(61, 58,55));
	setfillcolor(EGERGB(61, 58,55));  // 设置填充颜色
	ege_fillcircle(centerX, centerY, radius+20);  // 外圆
	
	setcolor(EGERGB(20, 20,20));
	setfillcolor(EGERGB(20, 20,20));  // 设置填充颜色
	ege_fillcircle(centerX, centerY, radius+9);  // 外圆
	
	setcolor(EGERGB(50, 50,50));
	setfillcolor(EGERGB(50, 50,50));  // 设置填充颜色
	ege_fillcircle(centerX, centerY, radius+7);  // 外圆
	
	setcolor(EGERGB(10, 10,10));
	setfillcolor(EGERGB(10, 10,10));  // 设置填充颜色
	ege_fillcircle(centerX, centerY, radius+1);  // 外圆
	
	drawScrew(centerX, centerY, radius);
}

//绘制刻度函数
void drawScale(float x, float y, float radius)
{
// 绘制刻度
	for (int i = 0; i <= 180; i += 10) 
	{
		double angle = i * PI / 180.0;
		if(i == 60 || i == 120)
		{
			int x1 = x + (radius - 35) * cos(angle);
			int y1 = y - (radius - 35) * sin(angle);
			int x2 = x + (radius - 10) * cos(angle);
			int y2 = y - (radius - 10) * sin(angle);
			setcolor(WHITE);
			setlinestyle(SOLID_LINE,0,10);
			ege_line(x1, y1, x2, y2);
			setfillcolor(WHITE);
		 	ege_fillellipse((float)x1-4.5, (float)y1-8, 9.0, 14.0);
		}			
		else if(i == 90)
		{
			int x1 = x + (radius - 45) * cos(angle);
			int y1 = y - (radius - 45) * sin(angle);
			int x2 = x + (radius - 10) * cos(angle);
			int y2 = y - (radius - 10) * sin(angle);
			setcolor(WHITE);
			setlinestyle(SOLID_LINE,0,20);
			ege_line(x1, y1, x2, y2);
			setfillcolor(WHITE);
			ege_fillellipse((float)x1-10, (float)y1-15, 20.0, 30.0);
		}	
	}	
}

// 绘制旋转后的椭圆
void drawEllipse(int ellipseCenterX, int ellipseCenterY, int a, int b, int rotationCenterX, int rotationCenterY, double angle) 
{
	int i;
	double x, y;
	// 设置抗锯齿相关属性
	setlinewidth(2); 
	setfillstyle(SOLID_FILL, WHITE); 	
	// 存储旋转后的点
	int points[720];
	for (i = 0; i < 360; i++)
	{
		// 计算椭圆上的点
		x = ellipseCenterX + a * cos(i * M_PI / 180);
		y = ellipseCenterY + b * sin(i * M_PI / 180);
		// 旋转点
		double dx = x - rotationCenterX;
		double dy = y - rotationCenterY;
		x = rotationCenterX + dx * cos(angle) - dy * sin(angle);
		y = rotationCenterY + dx * sin(angle) + dy * cos(angle);
		//将旋转好的点储存到数组里
		points[2 * i] = (int)x;
		points[2 * i + 1] = (int)y;
	}
	// 绘制填充的椭圆
	fillpoly(360, points);	
}

// 绘制仪表盘指针函数
void drawPointer(float centerX, float centerY, float radius, float angle) 
{
	double radian = angle * PI / 180.0;
	float x1 = centerX;
	float y1 = centerY;
//	int width = 30;  // 指针宽度
	int length = 100;  // 指针长度
	setcolor(WHITE);
	setlinestyle(SOLID_LINE, 0, 30);
	ege_line(x1, y1, x1 + length * 2 / 3 * cos(radian), y1 - length * 2 / 3 * sin(radian));
//	setfillcolor(WHITE);
//	ege_fillellipse((x1 - 30 / 2) * cos(radian) - (y1 - length - 25) * sin(radian), (x1 - 30 / 2) * sin(radian) + (y1 - length - 25) * cos(radian), 30.0, 50.0);
	drawEllipse((int)x1, (int)y1 - (length / 3 * 2), 15, 25, (int)x1, (int)y1, -radian);
	setcolor(EGERGB(50,50,50));
	setlinestyle(SOLID_LINE, 0, 30);
	ege_line(x1, y1, x1 + (length / 3) * cos(radian) , y1 - (length / 3) * sin(radian));
}

void drawDetails(float x, float y, float r)
{
	setfillcolor(EGERGB(50,50,50));
	ege_fillrect(x+70, y-30, 60.0, 35.0);
	setcolor(WHITE);
	setlinestyle(SOLID_LINE, 1);
	ege_line(x+70,y+3,x+130,y+3);
	ege_line(x+130,y+3,x+130,y-32);
}

void drawTurnDashboard(float centerX,float centerY, float radius)
{
	drawBaseDashboard(centerX, centerY, radius);
	drawBall(centerX, centerY, radius, 0.0);
	drawScale(centerX,centerY,radius);
	writeWord(centerX, centerY, radius);
	drawDetails(centerX,centerY,radius);
}

int main()
{
	// 初始化图形窗口
	initgraph(800, 600);
	ege_enable_aa(true);
	//仪表背景色
	setrendermode(RENDER_MANUAL);
	setbkcolor(EGERGB(190,190,190));
	cleardevice();
	float centerX = 400;
	float centerY = 300;
	float radius = 200;
	float angle = 30;
	// 绘制仪表盘
	while (!kbhit()) 
	{
		// 清屏
		cleardevice();
		// 重新绘制仪表盘
		drawTurnDashboard(centerX, centerY, radius);
		// 绘制指针
		drawPointer(centerX, centerY, radius, angle);
		// 更新角度
		angle += 1;
		// 延迟一段时间，控制转动速度
		delay_ms(50);
	}
	getch();
	closegraph();
	return 0;
}

